import gtk, pygtk
pygtk.require('2.0')

import socket, sys

class Whc:
    def __init__(self):
        """
        - Window Framework
        Create connect button which once clicked creates
        a new connection to the server using the callback
        function connectToServer
        
        """
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)
        self.window.set_title("DFS PANEL")
        self.window.set_default_size(500,500)
        self.window.set_border_width(10)
        self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.grid = gtk.Table(10,1,True)
        
        # Client & Admin Labels
        self.ClientLabel = gtk.Label("Client's Interface")
        self.AdminLabel = gtk.Label("Admin's Interface")
        # Connect button
        self.ConnectBtn = gtk.Button("Connect")
        self.ConnectBtn.connect("clicked", self.connectToServer, None)
        
        # Open button
        self.OpenBtn = gtk.Button("Open")
        self.OpenBtn.connect("clicked", self.openReq, None)
        
        # Read button
        self.ReadBtn = gtk.Button("Read")
        self.ReadBtn.connect("clicked", self.readReq, None)

        # Write button
        self.WriteBtn = gtk.Button("Write")
        self.WriteBtn.connect("clicked", self.writeReq, None)
        
        # Close button
        self.CloseBtn = gtk.Button("Close")
        self.CloseBtn.connect("clicked", self.closeReq, None)
        
        # Attach labels & buttons to window (grid)
        self.grid.attach(self.ClientLabel,0,1,0,1)
        self.grid.attach(self.ConnectBtn,0,1,1,2)
        self.grid.attach(self.OpenBtn,0,1,2,3)
        self.grid.attach(self.ReadBtn,0,1,3,4)
        self.grid.attach(self.WriteBtn, 0,1,4,5)
        self.grid.attach(self.CloseBtn,0,1,5,6)
        self.grid.attach(self.AdminLabel,0,1,6,7)

        self.window.add(self.grid)
        self.window.show_all()

    def connectToServer(self, widget, data=None):
        
        """
        Callback function to connect to server when the
        button is pressed.
        
        """
        
        # TODO: Think of a more clever way to give the address
        #       of the server
        HOST = '192.168.114.137'
        PORT = 1001
        
        s = None
        for res in socket.getaddrinfo(HOST, PORT, socket.AF_INET, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                s = socket.socket(af, socktype, proto)
            except socket.error, msg:
                s = None
                continue
            try:
                s.connect(sa)
            except socket.error, msg:
                s.close()
                s = None
                continue
            break
            
        #Error checking on establishing connection
        if s is None:
            print 'could not open socket'
            sys.exit(1)
        data = s.recv(1024)
        s.close()
        print 'Received', repr(data)

    def openReq(self):
        """
        TODO: Open File request. Cache the file requested
        """
        gtk.main()

    def readReq():
        """
        TODO: Read File request. Prints the content of the
        file requested
        """
        gtk.main()

    def writeReq():
        """
        TODO: Write File request. Opens a dialog box and the
        user types content. Option to save it.
        """
        gtk.main()

    def closeReq():
        """
        TODO: Close File request. Closes file requested.
        """
        gtk.main()

    def destroy(self, widget, data=None):
        """
        Exit DFS Panel
        """
        gtk.main_quit()

    def main(self):
        """
        Main function needed by all gtk windows
        """
        gtk.main()

if __name__ == "__main__":
    base = Whc()
    base.main()